<?php

namespace app\admin\controller\separate_accounts;

use app\admin\model\Store;
use app\common\controller\Backend;
use think\Db;
use think\Request;
use think\Session;

class Group extends Backend
{
    protected $searchFields = 'id,name';
    /**
     * @var mixed
     */
    private $service_secret;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->model = new \app\admin\model\Group();
        $this->service_secret = Session::get('admin')['secret'];
    }
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with('store')
                ->where($where)
                ->where(['group.service_secret'=>$this->service_secret])
                ->order($sort, $order)
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }
    public function store_list(){
        //设置过滤方法

        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $list = $this->model
            ->where($where)
            ->where(['service_secret'=>$this->service_secret])
            ->order($sort, $order)
            ->paginate($limit);
        $result = array("total" => $list->total(), "rows" => $list->items());
        return json($result);

    }
    public function add()
    {
        if ($this->request->isAjax()){
            $params = $this->request->param("row/a");
            $commission = Db::name("group")->where(['store_id'=>$params['store_id']])
                ->sum("commission");
            if ($commission > 100){
                $this->error("比例总和不能大于100");
            }
        }
        return parent::add();
    }
}